package com.mca.framework.lambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: zhoupeng
 * @createTime: 2025年07月14日 22:18:31
 * @version: 1.0
 * @Description:
 * @copyright:
 */
public class LambdaTest {

    public static void main(String[] args) {

        String[] language = {"c", "c++",
                "c#",
                "java","python",
                "go","hive",
                "php"};
        List<String> languageList = Arrays.asList(language);
        Set<String> set=new HashSet<>(Arrays.asList(language));
        //旧的循环方式
        for (String s : languageList) {
            System.out.println(s+",");
        }
        //lambda完成集合遍历   forEach低层就是使用迭代器实现的  Iterable
//        对实现了 Iterable 接口的集合的处理
//        当 foreach 循环用于处理实现了Iterable接口的集合类时，比如List、Set等，它的底层会使用迭代器来实现遍历。
//        @Override
//        public void forEach(Consumer<? super E> action) {
//            Objects.requireNonNull(action);
//            final int expectedModCount = modCount;
//            @SuppressWarnings("unchecked")
//            final E[] elementData = (E[]) this.elementData;
//            final int size = this.size;
//            for (int i=0; modCount == expectedModCount && i < size; i++) {
//                action.accept(elementData[i]);
//            }
//            if (modCount != expectedModCount) {
//                throw new ConcurrentModificationException();
//            }
//        }
        //数组索引遍历 或者 是迭代器遍历
        languageList.forEach(value-> System.out.println(value+","));

        Iterator<String> iterator = languageList.iterator();
        //使用迭代器遍历集合
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println("next低层使用："+next);
        }

        //lambda替换匿名内部类使用
        //匿名内部类
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello world !");
            }
        };

        Runnable runnableTemp=new Runnable() {
            @Override
            public void run() {
                System.out.println("---runnableTemp---");
            }
        };

        //lambda改造匿名内部类使用
        Runnable runnable1 = ()-> System.out.println("hello itheima lambda");

        Runnable runnable2=()-> System.out.println("hello itheima lambda 6666");

        //通过原有方式完成集合排序
        Arrays.sort(language,new Comparator<String>(){
            @Override
            public int compare(String o1, String o2) {
                return (o1.compareTo(o2));
            }
        });

        //lambda
        Arrays.sort(language,(o1,o2)->o1.compareTo(o2));
        //使用stream的sort排序
        List<String> streamSortList = languageList.stream().sorted(String::compareTo).collect(Collectors.toList());
        System.out.println(streamSortList);

        List<String> streamReverseSortList = languageList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(streamReverseSortList);

    }
}
